package com.tianci.appstore.ui.mainpage;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.coocaa.x.app.framework.app.LiteAppApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.skyworth.framework.skysdk.ipc.SkyStartActivity;
import com.tianci.appstore.AppDetailActivity;
import com.tianci.appstore.GameZoneActivity;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.control.SkyMainPageController;
import com.tianci.appstore.ui.data.SkyAppBaseData;
import com.tianci.appstore.ui.data.SkyAppDetailData;
import com.tianci.appstore.ui.data.SkyMainAppData;
import com.tianci.appstore.ui.util.UICommon;
import com.tianci.appstore.ui.widget.AlwaysMarqueeTextView;
import com.tianci.appstore.ui.widget.CatchImageView;
import com.tianci.appstore.ui.widget.SkyBaseItemView;
import com.tianci.appstore.ui.widget.TipToast;
import com.tianci.appstore.utils.SkyConstants;
import com.tianci.appstore.utils.SkyNetImageCache;

import java.util.HashMap;
import java.util.Map;

public class MainSmallBlock extends SkyBaseItemView {
    private ImageView iconImage;
    private static int titleSize = SkyConstants.Dpi(35);
    private ImageView mVipView = null;
    protected TextView installedText;
    protected static final int installedTextSize = SkyConstants.Dpi(18);

    protected SkyAppDetailData.AppStatus status = SkyAppDetailData.AppStatus.NOT_DOWNLOADED;

    public MainSmallBlock(Context context) {
        super(context);
    }

    @Override
    protected void createView() {
        // TODO Auto-generated method stub
        iconImage = new CatchImageView(mContext);
        iconImage.setScaleType(ScaleType.FIT_XY);
        LayoutParams iconParams = new LayoutParams(iconWidth, iconWidth, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
        iconParams.topMargin = SkyConstants.Div(16);
        this.addView(iconImage, iconParams);

        titleText = new AlwaysMarqueeTextView(mContext);
        titleText.setGravity(Gravity.CENTER);
        titleText.setSelected(false);
        titleText.setTextSize(titleSize);
        titleText.setPadding(SkyConstants.Div(20), 0, SkyConstants.Div(20), 0);
        SkinManager.setTextColor(titleText, R.color.titleColor);
        LayoutParams titleParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
        titleParams.bottomMargin = SkyConstants.Div(5);
        this.addView(titleText, titleParams);
    }

    private void addVipView() {
        if (mVipView == null) {
            mVipView = new CatchImageView(mContext);
            LayoutParams params = new LayoutParams(SkyConstants.cornerIconWidth, SkyConstants.cornerIconWidth, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
            params.setMargins(0, SkyConstants.Div(12), 0, 0);
            mVipView.setLayoutParams(params);
            this.addView(mVipView);
        }
        if (installedText != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(SkyConstants.Div(85), SkyConstants.Div(37));
            params.leftMargin = SkyConstants.Div(97);
            params.topMargin = SkyConstants.Div(123);
            if (installedText.getParent() != null) {
                this.removeView(installedText);
                this.addView(installedText, params);
            } else {
                this.addView(installedText, params);
            }
        }
    }

    @Override
    public void update(SkyAppBaseData data) {
        // TODO Auto-generated method stub
        String iconUrl = "";
        if (this.data != null) {
            iconUrl = this.data.getIconUrl();
        }
        this.data = data;
        if (data.getTitle() != null)
            titleText.setText(data.getTitle());
        LogUtils.d("url", data.getIconUrl());
        if (!iconUrl.equals(data.getIconUrl())) {
            if (data.getIconUrl() != null) {
                if (SkyNetImageCache.getSkyNetImageCache().downloadUrl("", data.getIconUrl(), this, SkyConstants.iconWidth,
                        SkyConstants.iconWidth, SkyConstants.Div(37)))
                    iconImage.setImageBitmap(null);
                else
                    iconImage.setImageDrawable(SkinManager.getResDrawable(R.drawable.icon_default));
            } else {
                iconImage.setImageDrawable(SkinManager.getResDrawable(R.drawable.icon_default));
            }
        }
        if (((SkyMainAppData) data).getCornerIcon() != null && !((SkyMainAppData) data).getCornerIcon().equals("")) {
            if (SkyNetImageCache.getSkyNetImageCache().downloadUrl("", ((SkyMainAppData) data).getCornerIcon(), this, SkyConstants.iconWidth,
                    SkyConstants.iconWidth)) ;
        }
    }


    private void refreshStatus() {
        if (status == SkyAppDetailData.AppStatus.INSTALLED || status == SkyAppDetailData.AppStatus.INSTALLED_WITH_CAN_NOT_BE_UNINSTALLED) {
            if (installedText == null) {
                installedText = UICommon.newTextView(mContext, installedTextSize, R.color.defaultColor);
            }
            installedText.setBackgroundResource(R.drawable.icon_installed_bg);
            installedText.setText(R.string.installed);
            installedText.setWidth(SkyConstants.Div(85));
            installedText.setHeight(SkyConstants.Div(37));
            installedText.setGravity(Gravity.CENTER);
            if (installedText.getParent() == null) {
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(SkyConstants.Div(85), SkyConstants.Div(37));
                params.leftMargin = SkyConstants.Div(97);
                params.topMargin = SkyConstants.Div(123);
                this.addView(installedText, params);
            }
        } else {
            if (installedText != null && installedText.getParent() != null) {
                removeView(installedText);
            }
        }
    }


    public void updateStatus(SkyAppDetailData.AppStatus status) {
        this.status = status;
        refreshStatus();
    }

    @Override
    protected void refreshImage(String url, Bitmap result) {
        // TODO Auto-generated method stub
        LogUtils.d("the corner icon result url is " + ((SkyMainAppData) data).getCornerIcon());
        if (((SkyMainAppData) data).getCornerIcon() != null && ((SkyMainAppData) data).getCornerIcon().equals(url)) {
            addVipView();
            mVipView.setImageBitmap(result);
        } else {
            iconImage.setImageBitmap(result);
        }
    }

    @Override
    public void onClick(View arg0) {
        LogUtils.i("log","the index is " + mIndex);
        LogUtils.i("detail", "smallBlock, onClick, data = " + data.toString());
        if (!LiteAppApplication.checkNetworkConnection(TipToast.showNetIsNotConnectedToastRunnable))
            return;

        String strId = this.data.getId();
        if (strId == null || strId.trim().length() == 0)
            return;
        String type = ((SkyMainAppData) data).getType();
        LogUtils.d("zone", "onClick  type:" + type + " " + this.data.getId());
        Intent intent;
        if (type != null && type.contains("1")) {
            intent = new Intent(mContext, GameZoneActivity.class);
            intent.putExtra("id", this.data.getId());
//            mContext.startActivity(intent);
        } else {
            intent = new Intent(mContext, AppDetailActivity.class);
            intent.putExtra("id", this.data.getId());
//            mContext.startActivity(intent);
        }
        HashMap<String,String> map = new HashMap<String,String>();
        SkyStartActivity.startActivity(mContext, mContext.getClass().getName(), intent, map, true);
        Map<String,String> params = new HashMap<String, String>();
        params.put("category", SkyMainPageController.getCurrentCategoryData().name + "页");
        params.put("id",String.valueOf(mIndex -2));
        params.put("block_content_name",data.getTitle());
        params.put("block_content_id",data.getPackageName());
        LogUtils.submit(LogUtils.PRODUCTID_APPSTORE, "as2_main_block_onclick", params);
        super.onClick(arg0);
    }

    @Override
    public void destory() {
        // TODO Auto-generated method stub
        if (iconImage != null)
            iconImage.setImageBitmap(null);
        if (mVipView != null)
            mVipView.setImageBitmap(null);
    }
}
